package com.gitee.wsl.ext.number

import kotlin.math.pow


/** Returns the nearest larger power of 2 for integer value. */
fun nearestPo2(value: Int) = nearestPo2(value.toLong()).toInt()

/** Returns the nearest larger power of 2 for long value. */
fun nearestPo2(value: Long): Long {
    val powerOf = kotlin.math.ceil(kotlin.math.ln(value.toDouble()) / kotlin.math.ln(2.0))
    return 2.0.pow(powerOf).toLong()
}

/** Returns the nearest larger power of 2 for float value. */
fun nearestPo2(value: Float) = nearestPo2(value.toDouble()).toFloat()

/** Returns the nearest larger power of 2 for double value. */
fun nearestPo2(value: Double): Double {
    val powerOf = kotlin.math.ceil(kotlin.math.ln(value) / kotlin.math.ln(2.0))
    return 2.0.pow(powerOf)
}